<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  function arr(nums) {
    let ans = nums[0];
    let sum = 0;
    for (const num of nums) {
      if (sum > 0) {
        sum += num;
      } else {
        sum = num;
      }
      ans = Math.max(ans, sum);
    }
    return ans;
  };
  console.log(arr(nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]));
</script>

</html>